package org.boyan.cases;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.boyan.entity.query.*;
import org.boyan.entity.trans.Trans;
import org.boyan.query.ExcelToOnlineDeviseScheme;
import org.boyan.query.ExcelToStrutDevise;
import org.boyan.trans.ExcelToTrans;
import org.boyan.utils.ExcelUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelToCaseBody {

    //根据导入的excel文件解析成List<Map<String,Object>的方式
    //List中的Map对应为多笔交易，其中应包含Map<'trans'>,Map<'StrutDevise'>,Map<'onlineDeviseScheme'>三个key，对应为三个sheet
    public static List<Map<String,Object>> getCaseBody(Object fileObject){
        Workbook workbook=null;
        if(fileObject instanceof MultipartFile){
            MultipartFile file= (MultipartFile) fileObject;
            workbook= ExcelUtils.getWorkBook(file);
        }else if(fileObject instanceof File){
            File file= (File) fileObject;
            workbook=ExcelUtils.getWorkBook(file);
        }
        List<Map<String,Object>> caseBodyList=new ArrayList<>();
        try{
            for(int i=3;i<workbook.getNumberOfSheets();i++){
                Sheet sheet=workbook.getSheetAt(i);
                if(sheet.getSheetName().contains("查询接口与逻辑")){
                    Map<String,Object> map=new HashMap<>();
                    Trans trans= ExcelToTrans.getTrans(sheet);
                    //为交易添加caseOrder
                    trans.setTransOrder((i/3)-1);
                    map.put("trans",trans);
                    Sheet strutDeviseSheet=workbook.getSheetAt(i+1);
                    Sheet onlineDeviseSheet=workbook.getSheetAt(i+2);
                    StrutDevise strutDevise= ExcelToStrutDevise.getStrutDevise(strutDeviseSheet);
                    OnlineDeviseScheme onlineDeviseScheme= ExcelToOnlineDeviseScheme.getOnlineDeviseScheme(onlineDeviseSheet);
                    map.put("strut",strutDevise);
                    map.put("online",onlineDeviseScheme);
                    caseBodyList.add(map);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return caseBodyList;
    }

}
